package com.drinks.yiniang.ui.fragment.user.wish;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.drinks.yiniang.R;
import com.drinks.yiniang.adapter.user.wish.DrinksWishAdapter;
import com.drinks.yiniang.common.recycler.decoration.DrinksWishStaggeredItemDecoration;
import com.drinks.yiniang.entity.user.wish.DrinksWishJson;
import com.drinks.yiniang.io.api.WishApiIO;
import com.drinks.yiniang.ui.activity.user.wine.WineListDetailActivity;
import com.drinks.yiniang.ui.widget.recycler.BaseRecyclerView;
import com.drinks.yiniang.utils.ToastUtils;
import com.hansen.library.Constants;
import com.hansen.library.io.tuple.Tuple2;
import com.hansen.library.listener.APIRequestCallback;
import com.hansen.library.ui.fragment.BaseLazyFragment;
import com.hansen.library.utils.CommonUtils;
import com.hansen.library.utils.StringUtils;

import java.util.ArrayList;

/**
 * 酒单
 * @author han
 * @date 2019/09/08
 * A simple {@link Fragment} subclass.
 */
public class DrinksWishFragment extends BaseLazyFragment implements BaseQuickAdapter.OnItemClickListener,
        BaseQuickAdapter.RequestLoadMoreListener {

    //列表
    private BaseRecyclerView rcv_drinks_wish;

    private DrinksWishAdapter mDrinksWishAdapter;

    //我的心愿酒单类型 1商品 2酒单 3他人酒单
    private String mWineType;
    private int mPages;

    public DrinksWishFragment() {
        // Required empty public constructor
    }


    public static DrinksWishFragment newInstance(String mWineType) {

        Bundle bundle = new Bundle();
        bundle.putString(Constants.KeyType, mWineType);

        DrinksWishFragment fragment = new DrinksWishFragment();
        fragment.setArguments(bundle);

        return fragment;
    }

    @Override
    protected int getResID() {
        return R.layout.fragment_drinks_wish;
    }

    @Override
    protected void initView(View mView) {
        rcv_drinks_wish = mView.findViewById(R.id.rcv_drinks_wish);
    }

    @Override
    protected void initData() {
        mContext = getActivity();

        mWineType = getArguments() != null ? getArguments().getString(Constants.KeyType) : null;

        initAdapter();
    }

    private void initAdapter() {
        //2.声名为瀑布流的布局方式: 2列 ,垂直方向
        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        //防止条目闪动
        staggeredGridLayoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);

        rcv_drinks_wish.setLayoutManager(staggeredGridLayoutManager);
        rcv_drinks_wish.addItemDecoration(new DrinksWishStaggeredItemDecoration(mContext, 10));

        mDrinksWishAdapter = new DrinksWishAdapter(mContext, new ArrayList<DrinksWishJson.DrinksWishList>());
        mDrinksWishAdapter.setCommonEmpty(getLayoutInflater(), rcv_drinks_wish);
        mDrinksWishAdapter.bindToRecyclerView(rcv_drinks_wish);
    }

    @Override
    protected void listener() {
        mDrinksWishAdapter.setOnItemClickListener(this);
        mDrinksWishAdapter.setOnLoadMoreListener(this, rcv_drinks_wish);
    }

    @Override
    protected void widgetClick(View v) {

    }

    @Override
    protected void lazyLoadData() {
        getWishWineList();
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        DrinksWishJson.DrinksWishList data = mDrinksWishAdapter.getItem(position);

        if(data == null) {
            return;
        }

        Intent intent = new Intent(mContext, WineListDetailActivity.class);
        intent.putExtra(Constants.KeyId, data.getId());
        startActivity(intent);
    }

    @Override
    public void onLoadMoreRequested() {
        WishApiIO.getInstance().getMyWishWineList(mWineType, mPages, new APIRequestCallback<DrinksWishJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(DrinksWishJson res) {
                if(mDrinksWishAdapter == null) {
                    return;
                }

                mPages = 1;
                mDrinksWishAdapter.addData(res.getData().getData());

                if(CommonUtils.isHasMoreData(res.getData().getData())) {
                    mDrinksWishAdapter.loadMoreComplete();
                } else {
                    mDrinksWishAdapter.loadMoreEnd();
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
                mDrinksWishAdapter.loadMoreFail();
            }

            @Override
            public void onComplete() {
            }
        });
    }

    //酒单列表
    private void getWishWineList() {
        if(StringUtils.isEmpty(mWineType)) {
            ToastUtils.showShort(R.string.text_wine_type_error);
            return;
        }

        showLoadingDialog();

        mPages = 1;

        WishApiIO.getInstance().getMyWishWineList(mWineType, mPages, new APIRequestCallback<DrinksWishJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(DrinksWishJson res) {
                if(mDrinksWishAdapter == null) {
                    return;
                }

                mPages = 1;
                mDrinksWishAdapter.getData().clear();
                mDrinksWishAdapter.addData(res.getData().getData());

                if(CommonUtils.isHasMoreData(res.getData().getData())) {
                    mDrinksWishAdapter.loadMoreComplete();
                } else {
                    mDrinksWishAdapter.loadMoreEnd();
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
                mDrinksWishAdapter.loadMoreFail();
            }

            @Override
            public void onComplete() {
            dismissDialog();
            }
        });
    }
}
